﻿

namespace Mulala.Cavan.Domain.MainBoundedContext.NannyModule
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using FluentValidation;
    using Mulala.Cavan.Domain.MainBoundedContext.Resources;
    using Mulala.Cavan.Core.Helper;
    using FluentValidation.Validators;
    using Mulala.Cavan.Core.Extension;
    using System.Text.RegularExpressions;

    public class NannyValidator : AbstractValidator<Nanny>
    {
        public NannyValidator()
        {
            RuleFor(n => n.Corporation).NotNull().WithMessage(Messages.Validate_NannyCorporationNotNull);
            RuleFor(n => n.Area).NotNull().WithMessage(Messages.Validate_NannyAreaNotNull);
            RuleFor(n => n.Name).NotEmpty().Length(2, 10).WithMessage(Messages.Validate_NannyNameIsInvalid);
            RuleFor(n => n.Address).Length(2, 50).WithMessage(Messages.Validate_AddressIsInvalid);
            //RuleFor(n => n.Telephone).SetValidator(new NannyTelephoneMobileValidator<Nanny>());
            RuleFor(n => n.QQ).Matches(StringHelper.NUMBERONLYREGULAR).WithMessage(Messages.Validate_NannyQQIsInvalid);
            RuleFor(n => n.Introduce).Length(2, 2000).WithMessage(Messages.Validate_NannyIntroduceIsInvalid);
            RuleFor(n => n.Declaration).Length(2, 2000).WithMessage(Messages.Validate_NannyDeclarationIsInvalid);
        }

        private class NannyTelephoneMobileValidator<T> : PropertyValidator
        {
            public NannyTelephoneMobileValidator()
                : base(Messages.Validate_TelephoneMobileIsInvalid)
            {

            }

            protected override bool IsValid(PropertyValidatorContext context)
            {
                var nanny = (Nanny)context.PropertyValue;
                Regex regex = new Regex(StringHelper.TELEPHONEREGULAR);
                bool result = false;
                if (nanny.Telephone.IsNotEmpty() || nanny.Mobile.IsNotEmpty())
                {
                    if (nanny.Telephone.IsNotEmpty())
                    {
                        result = regex.IsMatch(nanny.Telephone);
                    }
                    if (nanny.Mobile.IsNotEmpty())
                    {
                        result = regex.IsMatch(nanny.Mobile);
                    }
                }
                return false;
            }
        }
    }
}
